package com.wcr.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcr.school.mapper.AdminMapper;
import com.wcr.school.mapper.TeacherMapper;
import com.wcr.school.pojo.Admin;
import com.wcr.school.pojo.Login;
import com.wcr.school.pojo.Student;
import com.wcr.school.pojo.Teacher;
import com.wcr.school.service.AdminService;
import com.wcr.school.service.TeacherService;
import com.wcr.school.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service("implTeacherService")
@Transactional
public class ImplTeacherService  extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Override
    public Teacher login(Login login) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",login.getUsername());
        queryWrapper.eq("password", MD5.encrypt(login.getPassword()));
        Teacher teacher = baseMapper.selectOne(queryWrapper);
        return teacher;
    }

    @Override
    public Teacher getTeacherById(Long userId) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        Teacher teacher = baseMapper.selectOne(queryWrapper);
        return teacher;
    }

    @Override
    public IPage<Teacher> getTeachers(Page<Teacher> page, Teacher teacher) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(teacher.getClazzName()))
            queryWrapper.like("clazz_name",teacher.getClazzName());
        if(!StringUtils.isEmpty(teacher.getName()))
            queryWrapper.like("name",teacher.getName());
        queryWrapper.orderByAsc("id");
        IPage<Teacher> iPage=baseMapper.selectPage(page,queryWrapper);
        return iPage;
    }
}
